<template>
  <div class="content">
    <jc-button
      :btn-arr="['reload', 'sub', 'remove', 'option', 'pass', 'batchFill']"
      :disab-arr="['close', 'cancel', 'using', 'com1', 'com2', 'freeze']"
      :fbillno="fbillno"
      url="tPrdWorkreport/queryWorkReportList"
      @refresh="handleGetUntreated"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @del="delPublic"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        table-selection
        sortable
        get-row-keys="fentryid"
        :get-summaries="getSummaries"
        show-summary
        @selectionChange="handleSelectAll"
        @clickRow="detailPurchase"
      >
        <!--订单编码-->
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="detailPurchase(col.scope.row)">{{ col.scope.row.fbillNo }}</el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
        <!--审核状态-->
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentstatus }}</el-tag>
        </template>
      </jc-table>
      <cardTab :tab-arr="tabArr" field="fname" @search="handleQueryBomList" />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  queryWorkReportList, workReportDelete,
  workReportAuSubmit, workReportSubmit,
  workReportUnAudit, workReportAudit
} from '@/api/production/process'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { toMxBmina, Forbiders } from '@/components/ToMxamineState'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  name: 'WorkOrder',
  components: {
    cardTab: () => import('@/components/cardTab'),
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      ftype: 46,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 50, // 每页显示多少条数据
      tabArr: [
        { label: '餐椅', name: '餐椅' },
        { label: '软体', name: '软体' },
        { label: '下架', name: '下架' }
      ],
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      fbillno: '',
      tableHeader: [
        { label: this.$t('market.fdate'), prop: 'fdate', width: '90' },
        { label: this.$t('shop.customerOrder'), prop: 'salkhddh', width: '120' },
        { label: this.$t('market.billNo'), type: 'tag', prop: 'fbillNo', width: '120' },
        { label: this.$t('market.craftnumber'), prop: 'fcraftnumber' },
        { label: this.$t('market.fcraftname'), prop: 'fcraftname' },
        { label: this.$t('market.fmodel2'), prop: 'fmodel' },
        { label: this.$t('market.fmaterial'), prop: 'fnumber', type: 'order', width: '110' },
        { label: this.$t('shop.fname2'), prop: 'fname', width: '100' },
        { label: this.$t('market.productmodel'), prop: 'fproductmodel', width: '100' },
        { label: this.$t('market.funit'), prop: 'funit', width: '80' },
        { label: this.$t('market.mobillNo'), prop: 'fmobillno', width: '100' },
        { label: this.$t('market.proOrderSeq'), prop: 'mofseq' },
        { label: this.$t('market.srcmoqty'), prop: 'fmoqty', align: 'right', width: '100' },
        { label: this.$t('market.pushqty'), prop: 'fcompleteqty', align: 'right', width: '100' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentstatus', width: '100' },
        { label: this.$t('market.tranches'), prop: 'fgroupname' },
        { label: this.$t('market.processname'), prop: 'fprocessname' },
        { label: this.$t('market.processnumber'), prop: 'fprocessnumber' },
        { label: this.$t('market.profqty'), prop: 'profqty', align: 'right', width: '100' },
        { label: this.$t('market.workqty'), prop: 'fqty', align: 'right', width: '80' },
        { label: this.$t('market.multiple'), prop: 'fnum', align: 'right', width: '80' },
        { label: this.$t('market.fprice'), prop: 'fprice', align: 'right', width: '80' },
        { label: this.$t('market.billamount'), prop: 'fsumprice', align: 'right', width: '80' },
        { label: this.$t('market.worker'), prop: 'fworker' }
      ],
      tableHeader1: [
        { label: this.$t('market.workqty'), prop: 'fworkqty' },
        { label: this.$t('market.note'), prop: 'fremark' },
        { label: this.$t('market.detailSeq'), prop: 'fseq' },
        { label: this.$t('market.billType'), prop: 'fbilltype' },
        { label: this.$t('market.moseq'), prop: 'fmoseq' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescription' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate' },
        { label: this.$t('market.modifier'), prop: 'fmodifier' },
        { label: this.$t('market.modifyDate'), prop: 'fmodifydate' },
        { label: this.$t('market.approver'), prop: 'fapprover' }
      ],
      proData: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val.map(item => {
            return (toMxBmina(item), Forbiders(item))
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '报工单列表')
        } else {
          queryWorkReportList({
            pageSize: 5000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return (toMxBmina(item), Forbiders(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '报工单列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取生产列表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = { pageNum: this.currentPage, pageSize: this.size, search: this.searCollData.concat(this.details) }
      const RES = await queryWorkReportList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.array.map(item => {
          return (toMxBmina(item), Forbiders(item))
        })
        this.total = RES.data.total
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 成品 原材料
    handleQueryBomList(ev) {
      this.searCollData = []
      this.currentPage = 1
      this.searCollData = ev
      this.searchSon = ev
      this.handleGetUntreated()
    },
    // 详情
    detailPurchase(row) {
      this.$router.push({ path: `/workOrderDetail/${row.fid}` })
    },
    handleSelectAll(val) {
      this.val = val
      this.fbillno = val[0].fbillNo
      this.selectVal = []
      this.fentryData = []
      this.fidsData = []
      val.forEach(res => {
        res.fbillNo && this.selectVal.push(res.fbillNo)
        this.fentryData.push(res.fentryId)
        this.fidsData.push(res.fid)
      })
      this.fidsData = Array.from(new Set(this.fidsData))
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectVal }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 审批通过
    async audit(ev) {
      const api = ev === 1 ? workReportAudit : workReportUnAudit
      this.toExamineFun(api)
    },
    // 提交、反提交
    async submit(ev) {
      const api = ev === 1 ? workReportSubmit : workReportAuSubmit
      this.toExamineFun(api)
    },
    // 删除
    delPublic() {
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const api = workReportDelete
        this.toExamineFun(api)
      })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;

  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
